package com.xh.fma.commodity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.xh.fma.commodity.entity.AttrAttrgroupRelationEntity;
import com.xh.fma.commodity.entity.AttrEntity;
import com.xh.fma.commodity.service.AttrAttrgroupRelationService;
import com.xh.fma.commodity.service.AttrService;
import com.xh.fma.commodity.service.CategoryService;
import com.xh.fma.commodity.vo.AttrGroupWithAttrsVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xh.fma.commodity.entity.AttrgroupEntity;
import com.xh.fma.commodity.service.AttrgroupService;
import com.xh.common.utils.PageUtils;
import com.xh.common.utils.R;

import javax.annotation.Resource;


/**
 * 家居商品属性分组
 *
 * @author sjf
 * @email xhyy7762@gmail.com
 * @date 2023-05-27 17:06:03
 */
@RestController
@RequestMapping("commodity/attrgroup")
public class AttrgroupController {

    @Resource
    private AttrgroupService attrgroupService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Resource
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrgroupService.queryPage(params);

        return R.ok().put("page", page);
    }




    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R listByIdNameCategory(@RequestParam Map<String, Object> params,
                                  @PathVariable Long categoryId){
        PageUtils page = attrgroupService.queryPageByIdNameCategory(params, categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("commodity:attrgroup:info")
    public R info(@PathVariable("id") Long id){
		AttrgroupEntity attrgroup = attrgroupService.getById(id);
        Long[] category = categoryService.selectCategory(attrgroup.getCategoryId());
        attrgroup.setCascadedCategoryId(category);
        return R.ok().put("attrgroup", attrgroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:attrgroup:save")
    public R save(@RequestBody AttrgroupEntity attrgroup){
		attrgroupService.save(attrgroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:attrgroup:update")
    public R update(@RequestBody AttrgroupEntity attrgroup){
		attrgroupService.updateById(attrgroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:attrgroup:delete")
    public R delete(@RequestBody Long[] ids){
		attrgroupService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/{attrGroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> relationAttr = attrgroupService.getRelationAttr(attrGroupId);
        return R.ok().put("data",relationAttr);
    }

    @PostMapping("/attr/relation/delete")
    public R deleteBatchRelation(@RequestBody AttrAttrgroupRelationEntity[] entities){
        attrAttrgroupRelationService.deleteBatchRelation(entities);
        return R.ok();
    }

    @GetMapping("/{attrGroupId}/noattr/relation")
    public R queryPageByRelation(@PathVariable("attrGroupId") Long attrGroupId,
                                 @RequestParam Map<String, Object> params){
        PageUtils pageUtils = attrService.queryPageByRelation(params, attrGroupId);
        return R.ok().put("page", pageUtils);
    }

    @PostMapping("/attr/relation")
    public R saveBatchRelation(@RequestBody AttrAttrgroupRelationEntity[] entities){
        attrAttrgroupRelationService.saveBatch(Arrays.asList(entities));
        return R.ok();
    }

    @GetMapping("/{catalogId}/withattr")
    public R getAttrGroupAttr(@PathVariable("catalogId")Long catalogId){
        List<AttrGroupWithAttrsVo> attrGroupAttr = attrgroupService.getAttrGroupAttr(catalogId);
        return R.ok().put("data",attrGroupAttr);
    }





}
